fix gpsapp copy_char_array that could leave part of a packet uninitialized. (#1301)
authortsteven4 <13596209+tsteven4@users.noreply.github.com>
Sat, 20 Jul 2024 17:35:52 +0000 (11:35 -0600)
committerGitHub <noreply@github.com>
Sat, 20 Jul 2024 17:35:52 +0000 (11:35 -0600)
* fix gpsapp copy_char_array that could leave part of a packet uninitialized.

* tidy up a bit.

* delete empty statement

jeeps/gpsapp.cc

index 4b1441ed8a4aa46f9a569dee7a9ad1650f8390dc..d56a603e13b915ea6fd463b3879cc3411a2e353e 100644 (file)
@@ -125,28 +125,31 @@ char      gps_save_string[GPS_ARB_LEN];
  * we uppercase the string because some models (III's and 12's) react
  * violently to lower case data.
  */
-typedef enum { UpperNo = 0, UpperYes = 1 } copycase;
+enum copycase { UpperNo, UpperYes };
 
 static
-void copy_char_array(UC** dst, char* src, int count, copycase mustupper)
+void copy_char_array(UC** dst, const char* src, int count, copycase mustupper)
 {
   UC* d = *dst;
-  int ocount =  count;
-  do {
+  int copied = 0;
+  // Copy up to count characters from the source to the desitnation.
+  for (int i = 0; i < count; ++i) {
     UC sc = *src++;
     if (sc == 0) {
-      while (count--) {
-        *d++ = ' ';
-      }
       break;
     }
     if (!isalnum(sc)) {
       continue;
-    } else {
-      *d++ = mustupper == UpperYes ? toupper(sc) : sc;
     }
-  } while (--count) ;
-  *dst += ocount;
+    *d++ = mustupper == UpperYes ? toupper(sc) : sc;
+    copied++;
+  }
+  // If necessary pad with space characters so that the total count
+  // of characters written to the destination is count.
+  for (int i = copied; i < count; ++i) {
+    *d++ = ' ';
+  }
+  *dst += count;
 }